﻿#include <wchar.h>
#include <string.h>
#include <windows.h>

//========================================================================
// CreateWindowEx
typedef HWND (WINAPI *LPFN_CreateWindowExA)(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID);
LPFN_CreateWindowExA SysCreateWindowExA;

HWND WINAPI MyCreateWindowEx(
  DWORD dwExStyle,      // extended window style
  LPCTSTR lpClassName,  // registered class name
  LPCTSTR lpWindowName, // window name
  DWORD dwStyle,        // window style
  int x,                // horizontal position of window
  int y,                // vertical position of window
  int nWidth,           // window width
  int nHeight,          // window height
  HWND hWndParent,      // handle to parent or owner window
  HMENU hMenu,          // menu handle or child identifier
  HINSTANCE hInstance,  // handle to application instance
  LPVOID lpParam        // window-creation data
)
{
	HWND h = SysCreateWindowExA(dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam);
	return h;
}

//========================================================================
//GetCommandLine
typedef LPSTR (WINAPI *LPFN_GetCommandLineA)(VOID);
LPFN_GetCommandLineA SysGetCommandLineA;
LPTSTR WINAPI MyGetCommandLineA(VOID)
{
	static char cmd[MAX_PATH*4]={0};
	if(cmd[0]!='\0')
	{
		//MessageBox(0,cmd,0,0);
		return cmd;
	}

	char cur_path[MAX_PATH] = {0};
	GetCurrentDirectoryA(MAX_PATH,cur_path);

	char ini_path[MAX_PATH*2] = {0};
	strcat(ini_path,cur_path);
	strcat(ini_path,"\\");
	strcat(ini_path,"Patch.ini");

	if(!GetPrivateProfileIntA("RePath","Enable",0,ini_path))
	{
		//MessageBox(0,"RePath,Enable",0,0);
		return SysGetCommandLineA();
	}

	char RePath_Dest[MAX_PATH*2] = {0};
	if(!GetPrivateProfileStringA("RePath","Dest","",RePath_Dest,MAX_PATH,ini_path))
	{
		//MessageBox(0,"RePath.Dest",0,0);
		return SysGetCommandLineA();
	}
	
	char* old = SysGetCommandLineA();
	strcpy(cmd,old);
	strcat(cmd," ");

	char parm[MAX_PATH*2] = {0};
	strcat(parm,cur_path);
	strcat(parm,"\\");
	strcat(parm,RePath_Dest);

	strcat(cmd,parm);
	//MessageBox(0,cmd,0,0);
	return cmd;
}

typedef LPWSTR (WINAPI *LPFN_GetCommandLineW)(VOID);
LPFN_GetCommandLineW SysGetCommandLineW;
LPWSTR WINAPI MyGetCommandLineW(VOID)
{
	static wchar_t cmd[MAX_PATH*4]={0};
	if(cmd[0]!=L'\0')
	{
		//MessageBoxW(0,cmd,0,0);
		return cmd;
	}

	wchar_t cur_path[MAX_PATH] = {0};
	GetCurrentDirectoryW(MAX_PATH,cur_path);

	wchar_t ini_path[MAX_PATH*2] = {0};
	wcscat(ini_path,cur_path);
	wcscat(ini_path,L"\\");
	wcscat(ini_path,L"Patch.ini");

	if(!GetPrivateProfileIntW(L"RePath",L"Enable",0,ini_path))
	{
		//MessageBoxW(0,L"RePath,Enable",0,0);
		return SysGetCommandLineW();
	}

	wchar_t RePath_Dest[MAX_PATH*2] = {0};
	if(!GetPrivateProfileStringW(L"RePath",L"Dest",L"",RePath_Dest,MAX_PATH,ini_path))
	{
		//MessageBoxW(0,L"RePath.Dest",0,0);
		return SysGetCommandLineW();
	}
	
	wchar_t* old = SysGetCommandLineW();
	wcscat(cmd,old);
	wcscat(cmd,L" ");

	wchar_t parm[MAX_PATH*2] = {0};
	wcscat(parm,cur_path);
	wcscat(parm,L"\\");
	wcscat(parm,RePath_Dest);

	wcscat(cmd,parm);
	//MessageBoxW(0,cmd,0,0);
	return cmd;
}

//========================================================================
// patch_main
typedef int (__stdcall *LPFN_hook_api)(char* _dll,char* _name,int _dest);
__declspec(dllexport) void __stdcall patch_main(void)
{
	LPFN_hook_api hook_api=(LPFN_hook_api)GetProcAddress(LoadLibrary("Helper"),"_hook_api@12");
	if(hook_api)
	{
		SysCreateWindowExA = (LPFN_CreateWindowExA)hook_api("user32","CreateWindowExA",(int)&MyCreateWindowEx);
		SysGetCommandLineA = (LPFN_GetCommandLineA)hook_api("kernel32","GetCommandLineA",(int)&MyGetCommandLineA);
		SysGetCommandLineW = (LPFN_GetCommandLineW)hook_api("kernel32","GetCommandLineW",(int)&MyGetCommandLineW);
	}	
}

//========================================================================
// DllMain
BOOL WINAPI DllMain(HANDLE hInstDLL, DWORD dwReason, LPVOID lpvReserved)
{
	if(dwReason==DLL_PROCESS_ATTACH)
		patch_main();
	return 1;
}
